home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
profile.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
5KB
|
150 lines
% ****************************************************************
% * *
% * This is the program constructed in chapter 11 of *
% * Object Oriented Programming with Simula by Bj|rn Kirkerud; *
% * *
% ****************************************************************
begin
! Data structure: ;
integer array small_word_count(1 : 20);
text array the_small_words(1 : 20);
integer number_of_small_words;
! Data invariants: ;
! 1: 1 <= number_of_small_words <= 20 ;
! 2: For small_word_number = 1, 2, , number_of_small_words:
! small_word_count(small_word_number) is equal to the number of times
! the_small_words(small_word_number) has been read from file;
procedure Enter_small_words;
! Lets the user enter the small words ;
begin text word;
number_of_small_words := 0;
word :- prompt_for_text("Enter the first small word> ", 20);
while word =/= notext do
begin
number_of_small_words := number_of_small_words + 1;
the_small_words(number_of_small_words) :- word;
small_word_count(number_of_small_words) := 0;
word :- if number_of_small_words = 20
then notext
else prompt_for_text("Enter another small word> ", 20);
end;
end of Enter_small_words;
procedure Read_words;
! Reads all words from file and counts occurrencies of the small words;
begin text word, word_buffer; character c; integer small_word_number;
inspect new infile(prompt_for_text("Name of the file? ", 30)) do
begin
! Generate a text object which is used to build words: ;
word_buffer :- blanks(50);
open(blanks(80));
while not lastitem do
begin
c := inchar;
if letter(c) then
begin ! c must be the start of a word;
word_buffer := notext; word_buffer.setpos(1);
while letter(c) do
begin
if word_buffer.more then word_buffer.putchar(c);
c := inchar;
end;
word :- word_buffer.strip;
for small_word_number := 1 step 1 until number_of_small_words
do if the_small_words(small_word_number) = word
then small_word_count(small_word_number)
:= small_word_count(small_word_number) + 1;
end of letter(c);
end of while not lastitem;
close;
end of inspect infile;
end of Read_words;
procedure Write_table;
! Writes a table which shows how many times each of the small words
! occurs in the file: ;
begin integer small_word_length, small_word_number;
! First find the length of the longest small word: ;
small_word_length := 0;
for small_word_number := 1 step 1 until number_of_small_words do
small_word_length := max(small_word_length,
the_small_words(small_word_number).length);
! Then write the table: ;
outimage;
for small_word_number := 1 step 1 until number_of_small_words do
begin
outtext("The word """ & the_small_words(small_word_number) & """");
setpos(13 + small_word_length); outtext("occurs");
outint(small_word_count(small_word_number), 4); outtext(" times.");
outimage;
end;
outimage;
end of Write_table;
procedure Write_histogram;
! Write an histogram which shows the relative frequencies
! of the small words: ;
begin
integer count_sum, small_word_number, small_word_length,
asterisk_number, max_asterisks;
real percentage;
! First count the total number of small words in the file: ;
count_sum := 0; small_word_length := 0;
for small_word_number := 1 step 1 until number_of_small_words do
begin
count_sum := count_sum + small_word_count(small_word_number);
small_word_length := max(small_word_length,
the_small_words(small_word_number).length);
end;
! Then write the histogram: ;
if count_sum > 0 then
begin
max_asterisks := 0;
for small_word_number := 1 step 1 until number_of_small_words do
max_asterisks := max(max_asterisks,
100 * small_word_count(small_word_number) / count_sum/2);
for small_word_number := 1 step 1 until number_of_small_words do
begin
outtext(the_small_words(small_word_number) & ":");
setpos(small_word_length + 3);
percentage := 100 * small_word_count(small_word_number) / count_sum;
for asterisk_number := 1 step 1 until percentage/2 do outchar('*');
setpos(small_word_length + 4 + max_asterisks);
outfix(percentage, 1, 5);
outtext(" of the small words");
outimage;
end;
setpos(small_word_length + 3); outtext("0 10 20 30 40");
outimage; outimage;
end;
end of Write_histogram;
! An auxiliary procedure: ;
text procedure prompt_for_text(prompt, max_text_length);
text prompt; integer max_text_length;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_text :- intext(max_text_length).strip;
end;
! Imperatives: ;
Enter_small_words;
Read_words;
Write_table;
Write_histogram;
end